package com.aierkeji.call.cms.service;

import java.util.List;
import java.util.function.Function;

import javax.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.dao.jpa.AkjCallClueFileDao;
import com.aierkeji.call.cms.dao.jpa.AkjCallClueHistroyDao;
import com.aierkeji.call.cms.entity.AkjCallClueFile;
import com.aierkeji.call.cms.entity.AkjCallUser;
import com.aierkeji.call.cms.entity.vo.AkjCallClueVo;
import com.aierkeji.call.cms.util.CsvUtils;
import com.aierkeji.call.cms.util.DateTimeUtils;

@Service
public class AkjCallFuntionService {
	private String[] clueList = { "姓名", "电话号码", "性别", "备注", "导入时间", "线索分组", "分配状态", "分配坐席", "分配时间", "最后呼叫时间", "最后呼叫状态",
			"最后呼叫结果", "通话备注" };
	private static AkjCallFuntionService akjCallFuntionService;
	
	@PostConstruct
	public void init() {
		akjCallFuntionService = this;
	}
	private final Logger log = LogManager.getLogger(getClass());

	public static AkjCallFuntionService build() {
		return akjCallFuntionService;
	}
	@Autowired
	private AkjCallClueFileDao akjCallClueFileDao;
	
	@Autowired
	AkjCallClueHistroyDao akjCallClueHistroyDao;
	
	Function<List<AkjCallClueVo>, String> writeClueCsvString = (List<AkjCallClueVo> list) -> {
		String str = CsvUtils.doExport(false, clueList);
		if (!CollectionUtils.isEmpty(list)) {
//			if(SecurityUtils.)
			AkjCallUser akjCallUser = null;
			if(ThreadContext.getSecurityManager()==null) {
			}else {
				Subject subject = SecurityUtils.getSubject();
				akjCallUser = (AkjCallUser) subject.getSession().getAttribute("loginUser");

			}
			for (AkjCallClueVo vo : list) {
				str += CsvUtils.doExport(false, vo.getName(), akjCallUser!=null && akjCallUser.checkRole(Contants.ROLE_ID_SALES)?vo.getPhoneEncty():vo.getPhone(),
						vo.getGender() == null ? "" : vo.getGender().name(), vo.getRemark(),
						CsvUtils.getCsvDateString() + DateTimeUtils.converDateToString(vo.getCreatedAt(),
								DateTimeUtils.DATE_PATTERN_TIMESTAMP) + CsvUtils.getCsvDateString(),
						vo.getGroupName(), vo.getAssignedAt() == null ? "未分配" : "已分配", vo.getAssignedName(),
						CsvUtils.getCsvDateString() + DateTimeUtils.converDateToString(vo.getAssignedAt(),
								DateTimeUtils.DATE_PATTERN_TIMESTAMP) + CsvUtils.getCsvDateString(),
						CsvUtils.getCsvDateString() + DateTimeUtils.converDateToString(vo.getLastCallAt(),
								DateTimeUtils.DATE_PATTERN_TIMESTAMP) + CsvUtils.getCsvDateString(),
						vo.getLastCallStatus() == null ? "" : vo.getLastCallStatus().getName(),
						vo.getLastCallResult() == null ? "" : vo.getLastCallResult().getName(),vo.getLastRemark());
			}
		}
		return str;
	};
	
	Function<List<AkjCallClueFile>, Integer> saveClueFileFunction = (List<AkjCallClueFile> list)->{
		 this.akjCallClueFileDao.saveAll(list);
		 return list.size();
	};
	
	public int deleteClueHistory  (Long leaderId){
		
		return this.akjCallClueHistroyDao.deleteClueHistroy(leaderId);
	}
}
